/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.container.service.impl;

import com.alien.admin.modules.container.domain.K8sPodMetricStatusHistory;
import com.alien.admin.utils.*;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.container.repository.K8sPodMetricStatusHistoryRepository;
import com.alien.admin.modules.container.service.K8sPodMetricStatusHistoryService;
import com.alien.admin.modules.container.service.dto.K8sPodMetricStatusHistoryDto;
import com.alien.admin.modules.container.service.dto.K8sPodMetricStatusHistoryQueryCriteria;
import com.alien.admin.modules.container.service.mapstruct.K8sPodMetricStatusHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-10-20
**/
@Service
@RequiredArgsConstructor
public class K8sPodMetricStatusHistoryServiceImpl implements K8sPodMetricStatusHistoryService {

    private final K8sPodMetricStatusHistoryRepository k8sPodMetricStatusHistoryRepository;
    private final K8sPodMetricStatusHistoryMapper k8sPodMetricStatusHistoryMapper;

    @Override
    public Map<String,Object> queryAll(K8sPodMetricStatusHistoryQueryCriteria criteria, Pageable pageable){
        Page<K8sPodMetricStatusHistory> page = k8sPodMetricStatusHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(k8sPodMetricStatusHistoryMapper::toDto));
    }

    @Override
    public List<K8sPodMetricStatusHistoryDto> queryAll(K8sPodMetricStatusHistoryQueryCriteria criteria){
        return k8sPodMetricStatusHistoryMapper.toDto(k8sPodMetricStatusHistoryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public K8sPodMetricStatusHistoryDto findById(Long mininute) {
        K8sPodMetricStatusHistory k8sPodMetricStatusHistory = k8sPodMetricStatusHistoryRepository.findById(mininute).orElseGet(K8sPodMetricStatusHistory::new);
        ValidationUtil.isNull(k8sPodMetricStatusHistory.getMinute(),"K8sPodMetricStatusHistory","minute",mininute);
        return k8sPodMetricStatusHistoryMapper.toDto(k8sPodMetricStatusHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public K8sPodMetricStatusHistoryDto create(K8sPodMetricStatusHistory resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setMinute(snowflake.nextId());
        return k8sPodMetricStatusHistoryMapper.toDto(k8sPodMetricStatusHistoryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(K8sPodMetricStatusHistory resources) {
        K8sPodMetricStatusHistory k8sPodMetricStatusHistory = k8sPodMetricStatusHistoryRepository.findById(resources.getMinute()).orElseGet(K8sPodMetricStatusHistory::new);
        ValidationUtil.isNull( k8sPodMetricStatusHistory.getMinute(),"K8sPodMetricStatusHistory","id",resources.getMinute());
        k8sPodMetricStatusHistory.copy(resources);
        k8sPodMetricStatusHistoryRepository.save(k8sPodMetricStatusHistory);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long mininute : ids) {
            k8sPodMetricStatusHistoryRepository.deleteById(mininute);
        }
    }

    @Override
    public void download(List<K8sPodMetricStatusHistoryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (K8sPodMetricStatusHistoryDto k8sPodMetricStatusHistory : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" deploymentName",  k8sPodMetricStatusHistory.getDeploymentName());
            map.put(" cpu",  k8sPodMetricStatusHistory.getCpu());
            map.put(" mem",  k8sPodMetricStatusHistory.getMem());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearHistoryData(Integer days) {
        Long minutes = DateUtil.getDaysMinute(days);
        k8sPodMetricStatusHistoryRepository.removeAllByMinuteBefore(minutes);
    }
}
